<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>陕西人才知识管理</title>
<script type="text/javascript">
	var userid;
	//查询符合条件的用户
	function searchUserRole() {
		$('#dg').datagrid('load', {
			user_id : $('#userid').val()
		});
	}
	
	
	function chooseRole(index) {
		$('#dg').datagrid('selectRow',index);
		var row = $('#dg').datagrid('getSelected');
		 userid = row.user_id;
		$("#dlg").dialog("open").dialog("setTitle", userid+"角色信息");
         cleanRows();
		ajaxUserId(userid);
	}
	//请求权限信息
	function ajaxUserId(index){
		
		$.ajax({
			url:"getRolesListArray",
			type:'POST',
			async:true,
			data:{user_id:index},
			success:function(value){
				var data = eval(value);
				if(value !="")
					selectRows(data);
			}
		});
	}
	
	//查询角色权限并选中
	function selectRows(values)
	{
		var flag = false;
		var rows = $("#userrole").datagrid("getRows");
		for(var i=0;i<rows.length;i++)
		{
		      var rowId = rows[i].roleid;
		      for(var j=0;j<values.length;j++)
		      {
		    	
		        if(rowId== values[j].role_id)
		        {
		          var index = $("#userrole").datagrid("getRowIndex",rows[i]);
		          $("#userrole").datagrid("checkRow",index);
		          flag = true;
		          break;
		        }
		      }
		      if(flag) break;
		    }
	}
	
	function cleanRows(){
		
		$("#userrole").datagrid("uncheckAll","none");
	}
	
	function saveUserRole() {
		
		var row = $('#userrole').datagrid('getSelected');
 		var roleid = row.roleid;
			$.ajax({
	            type: "POST",
	            url: "addUsersRole",
	            data: "userid="+userid+"&roleid="+roleid,
	            success: function (result) {
	            	if(result==1){
						$.messager.alert("系统提示","保存成功");
						$("#dlg").dialog("close");
						$("#dg").datagrid("reload");
					}else{
						$.messager.alert("系统提示","保存失败");
						return;
					}
	            }
	        });
	}

	function formatOper(val, row, index) {
		
		return '<a href="#" onclick="chooseRole('+index+ ')">修改角色</a>';
	}
	
	//关闭对话框
	function closeMessageDialog() {
		$("#dlg").dialog("close");
	}
</script>
<link rel="stylesheet" type="text/css"
	href="../../easyui-1.5/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css"
	href="../../easyui-1.5/themes/icon.css" />
<script type="text/javascript" src="../../easyui-1.5/jquery.min.js"></script>
<script type="text/javascript"
	src="../../easyui-1.5/jquery.easyui.min.js"></script>
<script type="text/javascript"
	src="../../easyui-1.5/locale/easyui-lang-zh_CN.js"></script>
</head>
<body style="margin: 5px">
	<table id="dg" title="权限分配" class="easyui-datagrid" 
		striped="true"  fitColumns="true"
	    pagination="true" rownumbers="true" singleSelect="true"
		url="getUserRoleList" fit="true" toolbar="#tb" style="height:300px">
		<thead>
			<tr>
				<th field="user_id" width="100" align="center">用户名</th>
				<th field="user_name" width="100" align="center">昵称</th>
				<th field="user_style" width="100" align="center">拥有的角色</th>
				<th field="_operate" width="50" align="center" formatter="formatOper">操作</th>
			</tr>
		</thead>
	</table>
		
	<div id="tb" style="padding-bottom: 15px; padding-top: 15px">
		<div>
			<form method="post">
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="userid"  placeholder="请输入用户名或者昵称或者角色名称..." 
					id="userid" size="20" style="width:300px;height:22px" /> 
					<a href="javascript:searchUserRole()" class="easyui-linkbutton" iconCls="icon-search" plain="true">查询</a>
			</form>
		</div>
	</div>
	<div id="dlg" class="easyui-dialog"
		style="width: 350px; height: 450px;" closed="true"
		buttons="#dlg-buttons">
		<form id="fm" method="post" style="height: 100%">
		<table  id="userrole"  title="角色列表" class="easyui-datagrid" fitColumns="true"
			striped="true"  rownumbers="true" singleSelect="true"
			url="getUserRole" fit="true" >
			<thead>
				<tr>
					<th field="cb" checkbox="true">全选</th>
					<th field="roleid" width="100" align="center" hidden="true"></th>
					<th field="rolename" width="100" align="center">角色名称</th>
				</tr>
			</thead>
		</table>
		</form>
	</div>

	<div id="dlg-buttons">
		<a href="javascript:saveUserRole()" class="easyui-linkbutton"
			iconCls="icon-ok">保存</a> 
			<a href="javascript:closeMessageDialog()"
			class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
	</div>

</body>
</html>